package com.kobe.game_20;

/**
 * 
 * A palindromic number reads the same both ways. The largest palindrome made
 * from the product of two 2-digit numbers is 9009 = 91 X 99.
 * 
 * Find the largest palindrome made from the product of two 3-digit numbers.
 * 
 * The result is : 906609
 * 
 * 
 */
public class Game_4 {

    public static void main(String[] args) {
        int palindromic;
        for (int i = 999;; i--) {
            palindromic = generatePalindromicNumber(i);
            if (canBeDividedBy2_3DigitNumber(palindromic)) {
                System.out.println("The result is : " + palindromic);
                break;
            }
        }
    }

    /**
     * to generate palindromic number
     */
    private static int generatePalindromicNumber(int in) {
        StringBuffer original = new StringBuffer(String.valueOf(in));
        StringBuffer temp = new StringBuffer(String.valueOf(in));
        original.append(temp.reverse());
        return Integer.valueOf(original.toString());
    }

    /**
     * to judge whether the palindromic number can be devided
     * by 2 3-digits numbers
     */
    private static boolean canBeDividedBy2_3DigitNumber(int in) {
        for (int i = 999; i >= 100; i--) {
            if (in % i == 0 && in / i < 1000) {
                return true;
            }
        }
        return false;
    }
}
